<template>
  <d2-container>
    <div class="d2-breadcrumb">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" class="SearchForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="UID">
            <el-input
              placeholder="请输入"
              type="text"
              v-model.trim="query.id"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号">
            <el-input
              placeholder="请输入"
              type="text"
              v-model.trim="query.wxPhone"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户状态">
            <el-select v-model="query.userStatus">
              <el-option label="禁用" :value="0"></el-option>
              <el-option label="正常" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="认证角色">
            <el-select v-model="query._userType">
              <el-option label="经纪人" value="agent"></el-option>
              <el-option label="房东" value="homeowner"></el-option>
              <el-option label="合伙人" value="partner"></el-option>
              <el-option label="企业" value="company"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="注册时间">
            <el-date-picker
              v-model="query.createTime"
              type="date"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-button type="primary" @click="search()" icon="el-icon-search"
          >查询</el-button
        >
        <el-button type="danger" icon="el-icon-refresh" @click="reset"
          >重置</el-button
        >
      </el-row>
    </el-form>
    <el-table border :data="tableData.list">
      <el-table-column label="ID" align="center" prop="id"></el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickName">
        <template slot-scope="scope">
          <el-avatar size="large" :src="scope.row.headimgUrl"></el-avatar>
        </template>
      </el-table-column>

      <el-table-column
        label="用户昵称"
        align="center"
        prop="nickName"
      ></el-table-column>

      <el-table-column
        label="手机号"
        align="center"
        prop="wxPhone"
      ></el-table-column>
      <el-table-column
        label="WXID"
        align="center"
        prop="openid"
      ></el-table-column>
      <el-table-column label="用户性别" align="center" prop="title">
        <template slot-scope="scope">
          {{ ["未知", "男", "女"][scope.row.sex] }}
        </template>
      </el-table-column>

      <el-table-column
        label="角色名称"
        align="center"
        prop="approveName"
      ></el-table-column>

      <el-table-column label="创建时间" align="center" prop="create_time">
        <template slot-scope="scope">
          {{ $moment(scope.row.createTime).format("YYYY-MM-DD hh:mm:ss") }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          {{ ["停用", "正常"][scope.row.userStatus] }}
        </template>
      </el-table-column> -->
      <el-table-column label="用户状态" align="center">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.userStatus == 0"
            v-model="scope.row.userStatus == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            @change="changeStatus(1, scope.row.id, 'userStatus')"
          >
          </el-switch>
          <el-switch
            v-if="scope.row.userStatus == 1"
            v-model="scope.row.userStatus == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :inactive-value="0"
            @change="changeStatus(0, scope.row.id, 'userStatus')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="经纪人权限" align="center">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.agentRole == 0"
            v-model="scope.row.agentRole == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            @change="changeStatus(1, scope.row.id, 'agentRole')"
          >
          </el-switch>
          <el-switch
            v-if="scope.row.agentRole == 1"
            v-model="scope.row.agentRole == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :inactive-value="0"
            @change="changeStatus(0, scope.row.id, 'agentRole')"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="园区权限" align="center">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.gardenRole == 0"
            v-model="scope.row.gardenRole == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            @change="changeStatus(1, scope.row.id, 'gardenRole')"
          >
          </el-switch>
          <el-switch
            v-if="scope.row.gardenRole == 1"
            v-model="scope.row.gardenRole == 1"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :inactive-value="0"
            @change="changeStatus(0, scope.row.id, 'gardenRole')"
          >
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="search"
      @current-change="search"
      :current-page.sync="page"
      :page-sizes="[10, 25, 50, 100]"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
    ></el-pagination>
  </d2-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      query: {},
      tableData: {
        list: [],
        total: 0,
      },
      page: 1,
      pageSize: 10,
    };
  },
  methods: {
    search() {
      let params = {
        pageNum: this.page,
        pageSize: this.pageSize,
        ...this.query,
      };
      if (params.createTime) {
        params.createTime = new Date(
          Date.parse(new Date(params.createTime)) + 8 * 3600 * 1000
        );
      }
      if (params.createTime) {
        params.createTime = new Date(
          Date.parse(new Date(params.createTime)) + 8 * 3600 * 1000
        );
      }
      if (this.query._userType) {
        params[this.query._userType] = 1;
      }
      this.$axios.post("/wxUser/list", params).then((result) => {
        if (result) {
          this.tableData = {
            list: result.data.list,
            total: result.data.total,
          };
        } else {
          this.$message.error(result.data.msg);
        }
      });
    },
    reset() {
      this.query = {};
      this.search();
    },
    changeStatus(status, id, key) {
      let params = {
        id,
      };
      params[key] = status;
      console.log("参数", params);

      this.$axios.post("/wxUser/update", params).then((res) => {
        if (res.code) {
          this.$message.success("操作成功");
          this.search();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
  created() {
    this.search();
  },
};
</script>

<style scoped lang="scss">
</style>
